package com.iafme.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iafme.dto.req.EnterpriseReqDTO;
import com.iafme.dto.res.EnterpriseResDTO;
import com.iafme.model.Enterprise;
import com.iafme.service.EnterpriseService;
import com.systemframework.annotation.OperateAnnotation;
import com.systemframework.common.BaseCommonInterface;
import com.systemframework.enums.BusinessType;
import com.systemframework.util.Result;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author summer
 * @version 1.0
 * @date 2025/2/20 14:39
 */
@RestController
@RequestMapping(value = "/enterprise")
@AllArgsConstructor
public class EnterpriseController implements BaseCommonInterface<EnterpriseReqDTO, EnterpriseResDTO> {


    private final EnterpriseService enterpriseService;


    @OperateAnnotation(businessType = BusinessType.INSERT,name = "企业合作")
    @SaCheckPermission("partner:enterprise:add")
    @Override
    public Result<String> save(@Valid EnterpriseReqDTO enterpriseReqDTO) {
        Enterprise enterprise = Convert.convert(Enterprise.class, enterpriseReqDTO);
        enterpriseService.save(enterprise);
        return Result.successResult();
    }

    @OperateAnnotation(businessType = BusinessType.UPDATE,name = "企业合作")
    @SaCheckPermission("partner:enterprise:edit")
    @Override
    public Result<String> update(@Valid EnterpriseReqDTO enterpriseReqDTO) {
        Enterprise enterprise = Convert.convert(Enterprise.class, enterpriseReqDTO);
        enterpriseService.updateById(enterprise);
        return Result.successResult();
    }

    @OperateAnnotation(businessType = BusinessType.DELETE,name = "企业合作")
    @SaCheckPermission("partner:enterprise:delete")
    @Override
    public Result<String> del(Integer id) {
        enterpriseService.removeById(id);
        return Result.successResult();
    }

    @Override
    public Result<Page<EnterpriseResDTO>> lists(EnterpriseReqDTO enterpriseReqDTO) {
        return Result.successResult(enterpriseService.lists(enterpriseReqDTO));
    }

    @Override
    public Result<EnterpriseResDTO> findById(Integer integer) {
        return null;
    }

    @OperateAnnotation(businessType = BusinessType.BATCHDELETE,name = "企业合作")
    @SaCheckPermission("partner:enterprise:delete")
    @Override
    public Result<String> batchDel(List<EnterpriseResDTO> list) {
        List<Integer> ids = list.stream().map(EnterpriseResDTO::getId).collect(Collectors.toList());
        enterpriseService.removeByIds(ids);
        return Result.successResult();
    }
}
